package com.holdtime.jtt809.taxi.module.web.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.holdtime.jtt809.taxi.module.entity.PlatformLoginLogDO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLoginLogPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLoginLogRespVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLoginLogSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.PlatformLoginLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 下级平台登陆日志")
@RestController
@RequestMapping("/taxi/platform-login-log")
@Validated
public class PlatformLoginLogController {

    @Resource
    private PlatformLoginLogService platformLoginLogService;

    @PostMapping("/create")
    @Operation(summary = "创建下级平台登陆日志")
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:create')")
    public CommonResult<String> createPlatformLoginLog(@Valid @RequestBody PlatformLoginLogSaveReqVO createReqVO) {
        return success(platformLoginLogService.createPlatformLoginLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新下级平台登陆日志")
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:update')")
    public CommonResult<Boolean> updatePlatformLoginLog(@Valid @RequestBody PlatformLoginLogSaveReqVO updateReqVO) {
        platformLoginLogService.updatePlatformLoginLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除下级平台登陆日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:delete')")
    public CommonResult<Boolean> deletePlatformLoginLog(@RequestParam("id") String id) {
        platformLoginLogService.deletePlatformLoginLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得下级平台登陆日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:query')")
    public CommonResult<PlatformLoginLogRespVO> getPlatformLoginLog(@RequestParam("id") String id) {
        PlatformLoginLogDO platformLoginLog = platformLoginLogService.getPlatformLoginLog(id);
        return success(BeanUtils.toBean(platformLoginLog, PlatformLoginLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得下级平台登陆日志分页")
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:query')")
    public CommonResult<PageResult<PlatformLoginLogRespVO>> getPlatformLoginLogPage(@Valid PlatformLoginLogPageReqVO pageReqVO) {
        PageResult<PlatformLoginLogDO> pageResult = platformLoginLogService.getPlatformLoginLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformLoginLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出下级平台登陆日志 Excel")
    @PreAuthorize("@ss.hasPermission('taxi:platform-login-log:export')")
    public void exportPlatformLoginLogExcel(@Valid PlatformLoginLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformLoginLogDO> list = platformLoginLogService.getPlatformLoginLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "下级平台登陆日志.xls", "数据", PlatformLoginLogRespVO.class,
                        BeanUtils.toBean(list, PlatformLoginLogRespVO.class));
    }

}